<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/1
 * Time: 16:02
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use think\Db;
use think\Loader;
use think\Request;

class System extends AdminBase
{
    public function index()
    {
        $chk = check_juri(request()->controller(), 'index', $this->_juri);
        if ($chk['status'])
        {
            //系统设置分组
            $systemGroup = Db::name('system_group')
                ->field('id,title')
                ->order('sort asc')
                ->select();
            return view('index', [
                'systemGroup'=>$systemGroup
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function saveConfig()
    {
        if ($this->request->isAjax())
        {
            $chk = check_juri(request()->controller(), 'saveConfig', $this->_juri);
            if ($chk['status'])
            {
                $data   = Request::instance()->param();
                $system = Loader::model('System');
                $system->saveConfig($data);
                $result = update_config();
                if ($result)
                {
                    return ['status'=>true, 'msg'=>'保存成功'];
                } else {
                    return ['status'=>false, 'msg'=>'配置文件更新失败'];
                }
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }

        }
    }
    /**
     * 添加配置项
     * @return array
     */
    public function itemAdd()
    {
        if (request()->isAjax())
        {
            $chk = check_juri(request()->controller(), 'itemAdd', $this->_juri);
            if ($chk['status'])
            {
                $data   = Request::instance()->param();
                $result = $this->validate($data, 'System.add');
                if ($result !== true)
                {
                    return ['status'=>false, 'msg'=>$result];
                }
                $system = Loader::model('System');
                $system->itemAdd($data);
                $data['more'] = explode(',', $data['more']);
                return ['status'=>true, 'msg'=>$data];
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }

        }
    }
}